package com.ronds.webtest.Listener;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.AbstractWebDriverEventListener;

public class WebEventListener extends AbstractWebDriverEventListener {

    public void beforeNavigateTo(String url, WebDriver driver) {
        System.out.println("打开前url: '" + url + "'");
    }

    public void afterNavigateTo(String url, WebDriver driver) {
        System.out.println("打开url:'" + url + "'");
    }

    public void beforeClickOn(WebElement element, WebDriver driver) {
        System.out.println("试图单击对象: " + splitElement(element));
    }

    public void afterClickOn(WebElement element, WebDriver driver) {
        System.out.println("单击对象: " + splitElement(element));
    }

    @Override
    public void afterChangeValueOf(WebElement element, WebDriver driver, CharSequence[] arg2) {
        // TODO Auto-generated method stub
        if(arg2!=null){
            System.out.println(splitElement(element)+"数值已改变");
        }
    }

    public void onException(Throwable error, WebDriver driver) {
        System.out.println("Error occurred: " + error);
    }

    //获取操作的控件字符串
    private String splitElement(WebElement element) {
        String str = element.toString().split("-> ")[1];
        return str.substring(0, str.length() - 1);
    }
}